package com.cloudfun.campusshare.controller;

import com.cloudfun.campusshare.common.constant.Code;
import com.cloudfun.campusshare.common.constant.RoleType;
import com.cloudfun.campusshare.common.model.dto.BidCreateDTO;
import com.cloudfun.campusshare.common.model.dto.BidModifyDTO;
import com.cloudfun.campusshare.common.model.dto.BidQueryDTO;
import com.cloudfun.campusshare.common.model.vo.BidDetailVO;
import com.cloudfun.campusshare.common.model.vo.BidListVO;
import com.cloudfun.campusshare.common.response.BaseResult;
import com.cloudfun.campusshare.common.response.BaseResultFactory;
import com.cloudfun.campusshare.common.response.PageResult;
import com.cloudfun.campusshare.common.security.AuthRequired;
import com.cloudfun.campusshare.service.BidService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * Created by Huqin on 2020/4/8
 */
@RestController
@RequestMapping("/v1/bid")
@Validated
@Api(value = "招标信息模块")
@AuthRequired(role = RoleType.ADMIN)
public class BidController {

    @Autowired
    private BidService bidService;

    @PostMapping
    @ApiOperation(value = "创建")
    public BaseResult<String> create(@ApiParam(value = "createDTO", required = true) @Validated({BidCreateDTO.Create.class}) @RequestBody BidCreateDTO createDTO) {
        bidService.create(createDTO);
        return BaseResultFactory.produceResult(Code.SUCCESS, "成功");
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public BaseResult<String> modify(@ApiParam(value = "modifyDTO", required = true) @Valid @RequestBody BidModifyDTO modifyDTO) {
        bidService.update(modifyDTO);
        return BaseResultFactory.produceResult(Code.SUCCESS, "成功");
    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    public BaseResult<String> delete(@ApiParam(value = "id", required = true) @RequestParam String id) {
        bidService.delete(id);
        return BaseResultFactory.produceResult(Code.SUCCESS, "成功");
    }

    @GetMapping("/search")
    @ApiOperation(value = "分页查询")
    @AuthRequired(role = {RoleType.ADMIN, RoleType.OPERATIONAL_MERCHANT})
    public BaseResult<PageResult<BidListVO>> search(@ApiParam(value = "queryDTO") @Valid BidQueryDTO queryDTO, Pageable pageable) {
        return BaseResultFactory.produceResult(Code.SUCCESS, bidService.search(queryDTO, pageable));
    }

    @GetMapping("/detail")
    @ApiOperation(value = "详情")
    @AuthRequired(role = {RoleType.ADMIN, RoleType.OPERATIONAL_MERCHANT})
    public BaseResult<BidDetailVO> detail(@ApiParam(value = "id", required = true) @RequestParam String id) {
        return BaseResultFactory.produceResult(Code.SUCCESS, bidService.detail(id));
    }

}
